//
//  WBNavigationController.m
//  WeatherBar
//
//  Created by yby on 2024/6/7.
//

#import "WBNavigationController.h"
#import "WBBaseVC.h"

@interface WBNavigationController () <UIGestureRecognizerDelegate>

@end

@implementation WBNavigationController

-(instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    if (self = [super initWithRootViewController:rootViewController]) {
        [self.navigationBar setTranslucent:NO];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakself = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakself;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated
{
    [super setNavigationBarHidden:hidden animated:animated];
}

#pragma mark - UIGestureRecognizerDelegate
//这个方法是在手势将要激活前调用：返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        
        BOOL var_prohibitReturn = YES;
        if ([self.viewControllers.lastObject isKindOfClass:[WBBaseVC class]]) {
            WBBaseVC *var_topVC = self.viewControllers.lastObject;
            var_prohibitReturn = var_topVC.var_prohibitReturn;
        }
        if (self.visibleViewController == [self.viewControllers objectAtIndex:0] || var_prohibitReturn) {
            return NO;
        }
    }
    //这里就是非右滑手势调用的方法啦，统一允许激活
    return YES;
}


/**
 *  当控制器, 拿到导航控制器(需要是这个子类), 进行压栈时, 都会调用这个方法
 *
 *  @param viewController 要压栈的控制器
 *  @param animated       动画
 */
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    // 拦截每一个push的控制器, 进行统一设置
    // 过滤第一个根控制器
    if (self.childViewControllers.count > 0) {

        viewController.hidesBottomBarWhenPushed = YES;
    }

    // 千万不要忘记写
    [super pushViewController:viewController animated:animated];
}

@end
